using UnityEngine;
using System.Collections;

/// <summary>
/// Bass class managing ennemy. This basic ennemy just stay on the level
/// </summary>
public class Turret : Ennemy 
{
	//protected static float m
	public GameObject FiringPos;
	
	public AudioClip[] ambientClip;
	
	protected float mTimeToShoot = 1.5f;
	
	protected float mLastFire;
	
	new protected void Start()
	{
		base.Start();
		
		mLastFire = 0;
		
		int lRand = Random.Range(0, ambientClip.Length);
		GetComponent<EnnemySound>().SetAmbient(ambientClip[lRand], 0.8f, 1.2f, 1.0f);
	}
	
	
	protected void Update()
	{
		//base.Update();
		
		mLastFire += Time.deltaTime;
		
		if(mLastFire > mTimeToShoot)
		{
			mLastFire = 0;
			GameObject lBullet = Resources.Load("bullet") as GameObject;
			
			GameObject lInst;
			
			/*if(transform.localScale.y > 0)
			   	lInst = Instantiate(lBullet, transform.position - new Vector3(collider.bounds.extents.x + 1, -collider.bounds.extents.y + 3, 0), lBullet.transform.rotation) as GameObject;
			else
				lInst = Instantiate(lBullet, transform.position - new Vector3(collider.bounds.extents.x + 1, collider.bounds.extents.y - 3, 0), lBullet.transform.rotation) as GameObject;
			*/
			
			lInst = Instantiate(lBullet, FiringPos.transform.position, lBullet.transform.rotation) as GameObject;
			
			Bullet lBulletScr = lInst.GetComponent<Bullet>();
			lBulletScr.acc_sensRight = false;
			lBulletScr.acc_playerOwned = false;
		}
	}
	
	protected override void PostSpawnProcess()
	{
		int lUp = Random.Range(0,2);
		Vector3 lPos = Vector3.zero;
		
		switch(lUp)
		{
		case 0://Down
			lPos = new Vector3(transform.position.x, mWalkableRect.Bottom, transform.position.z);
			break;
		case 1://up
			transform.localScale = new Vector3(transform.localScale.x, -transform.localScale.y, transform.localScale.z);
			lPos = new Vector3(transform.position.x, mWalkableRect.Top, transform.position.z);
			break;
		default:
				break;
		}
		
		transform.position = lPos;
		
	}
}
